<?php

namespace Ecmpay\Ecmpay\ecmpay;

use Ecmpay\Ecmpay\Base;

class alipay extends Base
{
    /**
     * PC支付
     * @param array $orderData
     * @param string $notify_url
     * @param string $return_url
     * @return void
     * @author QianLong <87498106@qq.com>
     * @date 2021-04-02 16:52:58
     * @editAuthor QianLong <87498106@qq.com>
     * @editDescription 
     * @editDate 2021-04-02 16:52:58
     * @doc https://doc.qnplat.com/detail?doc=4683630058798691/4685709434024713
     */
    public static function alipayForPc(array $orderData, string $return_url, string $notify_url = null)
    {
        if (!isset($orderData['trade_no']) || empty($orderData['trade_no'])) {
            throw new \Exception("缺少商户单号: trade_no");
        }
        if (!isset($orderData['total_amount']) || empty($orderData['total_amount'])) {
            throw new \Exception("缺少金额: total_amount");
        }
        if (!isset($orderData['subject']) || empty($orderData['subject'])) {
            throw new \Exception("缺少订单标题: subject");
        }
        if (!isset(self::$config['app_id']) || empty(self::$config['app_id'])) {
            throw new \Exception("初始化配置中缺少应用APPID: app_id");
        }
        if (!isset(self::$config['auth_app_id']) || empty(self::$config['auth_app_id'])) {
            throw new \Exception("初始化配置中授权的支付宝应用APPID: app_id");
        }
        $apiUrl = self::$ecmUrl.'alipay/pcPay';
        $postData['app_id'] = self::$config['app_id']; //应用APPID
        $postData['auth_app_id'] = self::$config['auth_app_id']; //授权的支付宝应用APPID
        $postData['trade_no'] = $orderData['trade_no'];
        $postData['total_amount'] = $orderData['total_amount'];
        $postData['subject'] = $orderData['subject'];
        $postData['notify_url'] = $notify_url;
        $postData['return_url'] = $return_url;
        $postData['sign'] = self::$lib::createSign($postData,self::$config['appSecret']);
        $result = self::$lib::httpRequest($apiUrl, $postData);
        $result2 = json_decode($result['data'], true);
        if (!empty($result2) && isset($result2['code'])) {
            throw new \Exception($result2['msg']);
        } else {
            return $result['data'];
        }
    }
    /**
     * H5支付
     * @param array $orderData
     * @param string $notify_url
     * @param string $return_url
     * @return void
     * @author QianLong <87498106@qq.com>
     * @date 2021-04-02 16:52:58
     * @editAuthor QianLong <87498106@qq.com>
     * @editDescription 
     * @editDate 2021-04-02 16:52:58
     * @doc https://doc.qnplat.com/detail?doc=4683630058798691/4689273557058296
     */
    public static function alipayForWap(array $orderData, string $return_url, string $notify_url = null)
    {
        if (!isset($orderData['trade_no']) || empty($orderData['trade_no'])) {
            throw new \Exception("缺少商户单号: trade_no");
        }
        if (!isset($orderData['total_amount']) || empty($orderData['total_amount'])) {
            throw new \Exception("缺少金额: total_amount");
        }
        if (!isset($orderData['subject']) || empty($orderData['subject'])) {
            throw new \Exception("缺少订单标题: subject");
        }
        if (!isset(self::$config['app_id']) || empty(self::$config['app_id'])) {
            throw new \Exception("初始化配置中缺少应用APPID: app_id");
        }
        if (!isset(self::$config['auth_app_id']) || empty(self::$config['auth_app_id'])) {
            throw new \Exception("初始化配置中授权的支付宝应用APPID: app_id");
        }
        $apiUrl = self::$ecmUrl.'alipay/wapPay';
        $postData['app_id'] = self::$config['app_id']; //应用APPID
        $postData['auth_app_id'] = self::$config['auth_app_id']; //授权的支付宝应用APPID
        $postData['trade_no'] = $orderData['trade_no'];
        $postData['total_amount'] = $orderData['total_amount'];
        $postData['subject'] = $orderData['subject'];
        $postData['notify_url'] = $notify_url;
        $postData['quit_url'] = $return_url;
        $postData['sign'] = self::$lib::createSign($postData,self::$config['appSecret']);
        $result = self::$lib::httpRequest($apiUrl, $postData);
        $result2 = json_decode($result['data'], true);
        if (!empty($result2) && isset($result2['code'])) {
            throw new \Exception($result2['msg']);
        } else {
            return $result['data'];
        }
    }
    /**
     * 用户扫商家码支付
     * @param array $orderData
     * @param string $notify_url
     * @param string $return_url
     * @return void
     * @author QianLong <87498106@qq.com>
     * @date 2021-04-02 16:52:58
     * @editAuthor QianLong <87498106@qq.com>
     * @editDescription 
     * @editDate 2021-04-02 16:52:58
     * @doc https://doc.qnplat.com/detail?doc=4683630058798691/4689284856533264
     */
    public static function alipayForQrcode(array $orderData, string $return_url, string $notify_url = null)
    {
        if (!isset($orderData['trade_no']) || empty($orderData['trade_no'])) {
            throw new \Exception("缺少商户单号: trade_no");
        }
        if (!isset($orderData['total_amount']) || empty($orderData['total_amount'])) {
            throw new \Exception("缺少金额: total_amount");
        }
        if (!isset($orderData['subject']) || empty($orderData['subject'])) {
            throw new \Exception("缺少订单标题: subject");
        }
        if (!isset(self::$config['app_id']) || empty(self::$config['app_id'])) {
            throw new \Exception("初始化配置中缺少应用APPID: app_id");
        }
        if (!isset(self::$config['auth_app_id']) || empty(self::$config['auth_app_id'])) {
            throw new \Exception("初始化配置中授权的支付宝应用APPID: app_id");
        }
        $apiUrl = self::$ecmUrl.'alipay/qrcodePay';
        $postData['app_id'] = self::$config['app_id']; //应用APPID
        $postData['auth_app_id'] = self::$config['auth_app_id']; //授权的支付宝应用APPID
        $postData['trade_no'] = $orderData['trade_no'];
        $postData['total_amount'] = $orderData['total_amount'];
        $postData['subject'] = $orderData['subject'];
        $postData['notify_url'] = $notify_url;
        $postData['return_url'] = $return_url;
        $postData['sign'] = self::$lib::createSign($postData,self::$config['appSecret']);
        $result = self::$lib::httpRequest($apiUrl, $postData);
        $result2 = json_decode($result['data'], true);
        if (!empty($result2) && isset($result2['code']) && $result2['code'] == 200) {
            return $result['data'];
        } else {
            throw new \Exception($result2['msg']);
        }
    }
    /**
     * 商家通过扫描用户出示的付款码进行支付
     * @param array $orderData
     * @param string $notify_url
     * @param string $return_url
     * @return void
     * @author QianLong <87498106@qq.com>
     * @date 2021-04-02 16:52:58
     * @editAuthor QianLong <87498106@qq.com>
     * @editDescription 
     * @editDate 2021-04-02 16:52:58
     * @doc https://doc.qnplat.com/detail?doc=4683630058798691/4689288176316250
     */
    public static function alipayForPaymentCode(array $orderData, string $notify_url = null)
    {
        if (!isset($orderData['trade_no']) || empty($orderData['trade_no'])) {
            throw new \Exception("缺少商户单号: trade_no");
        }
        if (!isset($orderData['total_amount']) || empty($orderData['total_amount'])) {
            throw new \Exception("缺少金额: total_amount");
        }
        if (!isset($orderData['subject']) || empty($orderData['subject'])) {
            throw new \Exception("缺少订单标题: subject");
        }
        if (!isset($orderData['scene']) || empty($orderData['scene'])) {
            throw new \Exception("缺少支付场景: scene");
        }
        if (!isset($orderData['auth_code']) || empty($orderData['auth_code'])) {
            throw new \Exception("缺少支付授权码: auth_code");
        }
        if (!isset(self::$config['app_id']) || empty(self::$config['app_id'])) {
            throw new \Exception("初始化配置中缺少应用APPID: app_id");
        }
        if (!isset(self::$config['auth_app_id']) || empty(self::$config['auth_app_id'])) {
            throw new \Exception("初始化配置中授权的支付宝应用APPID: app_id");
        }
        $apiUrl = self::$ecmUrl.'alipay/paymentCodePay';
        $postData['app_id'] = self::$config['app_id']; //应用APPID
        $postData['auth_app_id'] = self::$config['auth_app_id']; //授权的支付宝应用APPID
        $postData['trade_no'] = $orderData['trade_no'];
        $postData['total_amount'] = $orderData['total_amount'];
        $postData['subject'] = $orderData['subject'];
        $postData['scene'] = $orderData['scene'];
        $postData['auth_code'] = $orderData['auth_code'];
        $postData['notify_url'] = $notify_url;
        // $postData['return_url'] = $return_url;
        $postData['sign'] = self::$lib::createSign($postData,self::$config['appSecret']);
        $result = self::$lib::httpRequest($apiUrl, $postData);
        $result2 = json_decode($result['data'], true);
        if (!empty($result2) && isset($result2['code']) && $result2['code'] == 200) {
            return $result['data'];
        } else {
            throw new \Exception($result2['msg']);
        }
    }
    /**
     * 支付宝关闭支付订单
     * @param array $orderData
     * @param string $notify_url
     * @param string $return_url
     * @return void
     * @author QianLong <87498106@qq.com>
     * @date 2021-04-02 16:52:58
     * @editAuthor QianLong <87498106@qq.com>
     * @editDescription 
     * @editDate 2021-04-02 16:52:58
     * @doc https://doc.qnplat.com/detail?doc=4683630058798691/4699193402878998
     */
    public static function alipayTradeClose(array $orderData, string $notify_url = null)
    {
        if (!isset(self::$config['app_id']) || empty(self::$config['app_id'])) {
            throw new \Exception("初始化配置中缺少应用APPID: app_id");
        }
        if (!isset(self::$config['auth_app_id']) || empty(self::$config['auth_app_id'])) {
            throw new \Exception("初始化配置中授权的支付宝应用APPID: app_id");
        }
        $apiUrl = self::$ecmUrl.'alipay/tradeClose';
        $postData['app_id'] = self::$config['app_id']; //应用APPID
        $postData['auth_app_id'] = self::$config['auth_app_id']; //授权的支付宝应用APPID
        if (isset($orderData['trade_no']) && !empty($orderData['trade_no'])) {
            $postData['trade_no'] = $orderData['trade_no'];
        }
        if (isset($orderData['alipay_trade_no']) && !empty($orderData['alipay_trade_no'])) {
            $postData['alipay_trade_no'] = $orderData['alipay_trade_no'];
        }
        $postData['sign'] = self::$lib::createSign($postData,self::$config['appSecret']);
        $result = self::$lib::httpRequest($apiUrl, $postData);
        $result2 = json_decode($result['data'], true);
        if (!empty($result2) && isset($result2['code']) && $result2['code'] == 200) {
            return $result['data'];
        } else {
            throw new \Exception($result2['msg']);
        }
    }
    /**
     * App支付
     * @param array $orderData
     * @param string $notify_url
     * @return void
     * @author QianLong <87498106@qq.com>
     * @date 2021-05-07 12:07:27
     * @editAuthor QianLong <87498106@qq.com>
     * @editDescription 
     * @editDate 2021-05-07 12:07:27
     */
    public static function alipayForApp(array $orderData, string $notify_url = null)
    {
        if (!isset($orderData['trade_no']) || empty($orderData['trade_no'])) {
            throw new \Exception("缺少商户单号: trade_no");
        }
        if (!isset($orderData['total_amount']) || empty($orderData['total_amount'])) {
            throw new \Exception("缺少金额: total_amount");
        }
        if (!isset($orderData['subject']) || empty($orderData['subject'])) {
            throw new \Exception("缺少订单标题: subject");
        }
        if (!isset(self::$config['app_id']) || empty(self::$config['app_id'])) {
            throw new \Exception("初始化配置中缺少应用APPID: app_id");
        }
        if (!isset(self::$config['auth_app_id']) || empty(self::$config['auth_app_id'])) {
            throw new \Exception("初始化配置中授权的支付宝应用APPID: app_id");
        }
        $apiUrl = self::$ecmUrl.'alipay/appPay';
        $postData['app_id'] = self::$config['app_id']; //应用APPID
        $postData['auth_app_id'] = self::$config['auth_app_id']; //授权的支付宝应用APPID
        $postData['trade_no'] = $orderData['trade_no'];
        $postData['total_amount'] = $orderData['total_amount'];
        $postData['subject'] = $orderData['subject'];
        $postData['notify_url'] = $notify_url;
        // $postData['return_url'] = $return_url;
        $postData['sign'] = self::$lib::createSign($postData,self::$config['appSecret']);
        $result = self::$lib::httpRequest($apiUrl, $postData);
        $result2 = json_decode($result['data'], true);
        if (!empty($result2) && isset($result2['code'])) {
            throw new \Exception($result2['msg']);
        } else {
            return $result['data'];
        }
    }
    /**
     * 支付宝转帐接口
     * @param array $orderData
     * @return void
     * @author QianLong <87498106@qq.com>
     * @date 2021-05-07 12:07:27
     * @editAuthor QianLong <87498106@qq.com>
     * @editDescription 
     * @editDate 2021-05-07 12:07:27
     * @doc https://doc.qnplat.com/detail?doc=4683630058798691/4689625747281532
     */
    public static function transferAlipay(array $orderData)
    {
        if (!isset($orderData['trade_no']) || empty($orderData['trade_no'])) {
            throw new \Exception("缺少商户单号: trade_no");
        }
        if (!isset($orderData['trans_amount']) || empty($orderData['trans_amount'])) {
            throw new \Exception("缺少金额: trans_amount");
        }
        if (!isset($orderData['identity']) || empty($orderData['identity'])) {
            throw new \Exception("缺少参与方的标识 ID: identity");
        }
        if (!isset($orderData['subject']) || empty($orderData['subject'])) {
            throw new \Exception("缺少订单标题: subject");
        }
        if (!isset($orderData['name']) || empty($orderData['name'])) {
            throw new \Exception("缺少参与方真实姓名: name");
        }
        if (!isset($orderData['payer_show_name']) || empty($orderData['payer_show_name'])) {
            throw new \Exception("缺少转帐来源账户名: payer_show_name");
        }
        if (!isset($orderData['identity_type']) || empty($orderData['identity_type'])) {
            throw new \Exception("缺少参与方的标识类型: identity_type");
        }
        if (!isset(self::$config['app_id']) || empty(self::$config['app_id'])) {
            throw new \Exception("初始化配置中缺少应用APPID: app_id");
        }
        if (!isset(self::$config['auth_app_id']) || empty(self::$config['auth_app_id'])) {
            throw new \Exception("初始化配置中授权的支付宝应用APPID: app_id");
        }
        $apiUrl = self::$ecmUrl.'alipay/transferAlipay';
        $postData['app_id'] = self::$config['app_id']; //应用APPID
        $postData['auth_app_id'] = self::$config['auth_app_id']; //授权的支付宝应用APPID
        $postData['trade_no'] = $orderData['trade_no'];
        $postData['trans_amount'] = $orderData['trans_amount'];
        $postData['order_title'] = $orderData['subject'];
        $postData['identity'] = $orderData['identity'];
        $postData['identity_type'] = $orderData['identity_type'];
        $postData['name'] = $orderData['name'];
        $postData['payer_show_name'] = $orderData['payer_show_name'];
        if (isset($orderData['remark']) && !empty($orderData['remark'])) {
            $postData['remark'] = $orderData['remark'];
        }
        $postData['sign'] = self::$lib::createSign($postData,self::$config['appSecret']);
        $result = self::$lib::httpRequest($apiUrl, $postData);
        $result2 = json_decode($result['data'], true);
        if (!empty($result2) && isset($result2['code'])) {
            throw new \Exception($result2['msg']);
        } else {
            return $result['data'];
        }
    }
}
